home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / Mail / Queue / Container / db.php next >
PHP Script  |  2004-03-24  |  13KB  |  372 lines

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.txt.                                 |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Radek Maciaszek <chief@php.net>                             |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: db.php,v 1.9 2004/02/29 09:19:14 quipo Exp $
  20.  
  21. /**
  22.  * Storage driver for fetching mail queue data from a PEAR_DB database
  23.  *
  24.  * This storage driver can use all databases which are supported
  25.  * by the PEAR DB abstraction layer.
  26.  *
  27.  * @author   Radek Maciaszek <chief@php.net>
  28.  * @package  Mail_Queue
  29.  * @version  $Revision: 1.9 $
  30.  */
  31. require_once 'DB.php';
  32. require_once 'Mail/Queue/Container.php';
  33.  
  34. /**
  35. * Mail_Queue_Container_db - Storage driver for fetching mail queue data
  36. * from a PEAR_DB database
  37. *
  38. * @author   Radek Maciaszek <chief@php.net>
  39. * @version  $Id: db.php,v 1.9 2004/02/29 09:19:14 quipo Exp $
  40. * @package  Mail_Queue
  41. * @access   public
  42. */
  43. class Mail_Queue_Container_db extends Mail_Queue_Container
  44. {
  45.     // {{{ class vars
  46.  
  47.     /**
  48.      * Reference to the current database connection.
  49.      * @var object PEAR_DB
  50.      */
  51.     var $db;
  52.  
  53.     /**
  54.      * Table for sql database
  55.      * @var  string
  56.      */
  57.     var $mail_table = 'mail_queue';
  58.  
  59.     /**
  60.      * @var string  the name of the sequence for this table
  61.      */
  62.     var $sequence = null;
  63.  
  64.     // }}}
  65.     // {{{ Mail_Queue_Container_db()
  66.  
  67.     /**
  68.      * Contructor
  69.      *
  70.      * Mail_Queue_Container_db:: Mail_Queue_Container_db()
  71.      *
  72.      * @param mixed $options    An associative array of option names and
  73.      *                          their values. See DB_common::setOption
  74.      *                          for more information about connection options.
  75.      *
  76.      * @access public
  77.      */
  78.     function Mail_Queue_Container_db($options)
  79.     {
  80.         if (!is_array($options) || !isset($options['dsn'])) {
  81.             return new Mail_Queue_Error(MAILQUEUE_ERROR_NO_OPTIONS,
  82.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  83.                 'No dns specified!');
  84.         }
  85.         if (isset($options['mail_table'])) {
  86.             $this->mail_table = $options['mail_table'];
  87.         }
  88.         $this->sequence = (isset($options['sequence']) ? $options['sequence'] : $this->mail_table);
  89.  
  90.         if (!empty($options['pearErrorMode'])) {
  91.             $this->pearErrorMode = $options['pearErrorMode'];
  92.         }
  93.         $this->db =& DB::connect($options['dsn'], true);
  94.         if (DB::isError($this->db)) {
  95.             return new Mail_Queue_Error(MAILQUEUE_ERROR_CANNOT_CONNECT,
  96.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  97.                 'DB::connect failed: '. DB::errorMessage($this->db));
  98.         } else {
  99.             $this->db->setFetchMode(DB_FETCHMODE_ASSOC);
  100.         }
  101.         $this->setOption();
  102.     }
  103.  
  104.     // }}}
  105.     // {{{ _preload()
  106.  
  107.     /**
  108.      * Preload mail to queue.
  109.      *
  110.      * @param integer  $limit  Optional - Number of mails loaded to queue
  111.      * @param integer  $offset  Optional - You could also specify offset
  112.      * @param boolean  $force_preload  Optional - FIXME
  113.      *
  114.      * @return mixed  True on success else Mail_Queue_Error object.
  115.      *
  116.      * @access private
  117.      */
  118.     function _preload()
  119.     {
  120.         $query = sprintf("SELECT * FROM %s WHERE sent_time IS NULL
  121.                             AND try_sent < %d
  122.                             AND now() > time_to_send
  123.                             ORDER BY time_to_send",
  124.                          $this->mail_table,
  125.                          $this->try
  126.                          );
  127.         if ($this->limit != MAILQUEUE_ALL) {
  128.             $query .= " LIMIT $this->offset, $this->limit";  //THIS SYNTAX WORKS ON MYSQL ONLY!! FIX IT!
  129.         }
  130.         $res = $this->db->query($query);
  131.         if (DB::isError($res)) {
  132.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  133.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  134.                 'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  135.         }
  136.  
  137.         $i = 0;
  138.         $this->queue_data = array(); //reset buffer
  139.         while ($row = $res->fetchRow(DB_FETCHMODE_ASSOC)) {
  140.             if (!is_array($row)) {
  141.                 return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  142.                     $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  143.                     'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  144.             }
  145.             $this->queue_data[$i] = new Mail_Queue_Body(
  146.                 $row['id'],
  147.                 $row['create_time'],
  148.                 $row['time_to_send'],
  149.                 $row['sent_time'],
  150.                 $row['id_user'],
  151.                 $row['ip'],
  152.                 $row['sender'],
  153.                 $row['recipient'],
  154.                 unserialize($row['headers']),
  155.                 unserialize($row['body']),
  156.                 $row['delete_after_send'],
  157.                 $row['try_sent']
  158.             );
  159.         }
  160.         $i++;
  161.         return true;
  162.     }
  163.  
  164.     // }}}
  165.     // {{{ put()
  166.  
  167.     /**
  168.      * Put new mail in queue and save in database.
  169.      *
  170.      * Mail_Queue_Container::put()
  171.      *
  172.      * @param string $time_to_send  When mail have to be send
  173.      * @param integer $id_user  Sender id
  174.      * @param string $ip  Sender ip
  175.      * @param string $from  Sender e-mail
  176.      * @param string $to  Reciepient e-mail
  177.      * @param string $hdrs  Mail headers (in RFC)
  178.      * @param string $body  Mail body (in RFC)
  179.      * @param bool $delete_after_send  Delete or not mail from db after send
  180.      *
  181.      * @return mixed  ID of the record where this mail has been put
  182.      *                or Mail_Queue_Error on error
  183.      * @access public
  184.      **/
  185.     function put($time_to_send, $id_user, $ip, $sender,
  186.                 $recipient, $headers, $body, $delete_after_send=true)
  187.     {
  188.         $id = $this->db->nextId($this->sequence);
  189.         if (empty($id)) {
  190.             return new Mail_Queue_Error(MAILQUEUE_ERROR,
  191.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  192.                 'Cannot create id in: '.$this->sequence);
  193.         }
  194.         $query = sprintf("INSERT INTO %s (id, create_time, time_to_send, id_user, ip,
  195.                         sender, recipient, headers, body, delete_after_send)
  196.                         VALUES('%s', now(), '%s', %d, '%s', '%s', '%s', '%s', '%s', %d )",
  197.                          $this->mail_table,
  198.                          $id,
  199.                          addslashes($time_to_send),
  200.                          addslashes($id_user),
  201.                          addslashes($ip),
  202.                          addslashes($sender),
  203.                          addslashes($recipient),
  204.                          addslashes($headers),
  205.                          addslashes($body),
  206.                          $delete_after_send
  207.         );
  208.  
  209.         $res = $this->db->query($query);
  210.         if (DB::isError($res)) {
  211.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  212.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  213.                 'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  214.         }
  215. /*
  216.         $this->_last_item++;
  217.         $this->queue_data[$this->_last_item] = new Mail_Queue_Body(
  218.             $id,
  219.             date("d-m-y G:i:s"),
  220.             $time_to_send,
  221.             null,
  222.             $id_user,
  223.             $ip,
  224.             $sender,
  225.             $recipient,
  226.             unserialize($headers),
  227.             unserialize($body),
  228.             $delete_after_send,
  229.             0
  230.         );
  231. */
  232.         return $id;
  233.     }
  234.  
  235.     // }}}
  236.     // {{{ countSend()
  237.  
  238.     /**
  239.      * Check how many times mail was sent.
  240.      *
  241.      * @param object   Mail_Queue_Body
  242.      * @return mixed  Integer or Mail_Queue_Error class if error.
  243.      * @access public
  244.      */
  245.     function countSend($mail)
  246.     {
  247.         if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
  248.             return new Mail_Queue_Error('Expected: Mail_Queue_Body class',
  249.                 __FILE__, __LINE__);
  250.         }
  251.         $count = $mail->try();
  252.         $query = sprintf("UPDATE %s SET try_sent = %d WHERE id = %d",
  253.                          $this->mail_table,
  254.                          $count,
  255.                          $mail->getId()
  256.         );
  257.  
  258.         $res = $this->db->query($query);
  259.         if (DB::isError($res)) {
  260.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  261.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  262.                 'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  263.         }
  264.         return $count;
  265.     }
  266.  
  267.     // }}}
  268.     // {{{ setAsSent()
  269.  
  270.     /**
  271.      * Set mail as already sent.
  272.      *
  273.      * @param object Mail_Queue_Body object
  274.      * @return bool
  275.      * @access public
  276.      */
  277.     function setAsSent($mail)
  278.     {
  279.         if (!is_object($mail) || !is_a($mail, 'mail_queue_body')) {
  280.             return new Mail_Queue_Error(MAILQUEUE_ERROR_UNEXPECTED,
  281.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  282.                 'Expected: Mail_Queue_Body class');
  283.         }
  284.         $query = sprintf("UPDATE %s SET sent_time = now() WHERE id = %d",
  285.                          $this->mail_table,
  286.                          $mail->getId()
  287.         );
  288.  
  289.         $res = $this->db->query($query);
  290.         if (DB::isError($res)) {
  291.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  292.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  293.                 'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  294.         }
  295.         return true;
  296.     }
  297.  
  298.     // }}}
  299.     // {{{ getMailById()
  300.  
  301.     /**
  302.      * Return mail by id $id (bypass mail_queue)
  303.      *
  304.      * @param integer $id  Mail ID
  305.      * @return mixed  Mail object or false on error.
  306.      * @access public
  307.      */
  308.     function getMailById($id)
  309.     {
  310.         $query = sprintf("SELECT * FROM %s WHERE id = %d",
  311.                          $this->mail_table,
  312.                          addslashes($id)
  313.         );
  314.         $res = $this->db->query($query);
  315.  
  316.         if (DB::isError($res)) {
  317.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  318.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  319.                 'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  320.         }
  321.         $row = $res->fetchRow(DB_FETCHMODE_ASSOC);
  322.         if (!is_array($row)) {
  323.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  324.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  325.                 'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  326.         }
  327.         return new Mail_Queue_Body(
  328.             $row['id'],
  329.             $row['create_time'],
  330.             $row['time_to_send'],
  331.             $row['sent_time'],
  332.             $row['id_user'],
  333.             $row['ip'],
  334.             $row['sender'],
  335.             $row['recipient'],
  336.             unserialize($row['headers']),
  337.             unserialize($row['body']),
  338.             $row['delete_after_send'],
  339.             $row['try_sent']
  340.         );
  341.     }
  342.  
  343.     // }}}
  344.     // {{{ deleteMail()
  345.  
  346.     /**
  347.      * Remove from queue mail with $id identifier.
  348.      *
  349.      * @param integer $id  Mail ID
  350.      * @return bool  True on success else Mail_Queue_Error class
  351.      *
  352.      * @access public
  353.      */
  354.     function deleteMail($id)
  355.     {
  356.         $query = sprintf("DELETE FROM %s WHERE id = %d",
  357.                          $this->mail_table,
  358.                          addslashes($id)
  359.         );
  360.         $res = $this->db->query($query);
  361.  
  362.         if (DB::isError($res)) {
  363.             return new Mail_Queue_Error(MAILQUEUE_ERROR_QUERY_FAILED,
  364.                 $this->pearErrorMode, E_USER_ERROR, __FILE__, __LINE__,
  365.                 'DB::query failed - "'.$query.'" - '.DB::errorMessage($res));
  366.         }
  367.         return true;
  368.     }
  369.  
  370.     // }}}
  371. }
  372. ?>